/**
 * 校验器
 */

const { LinValidator, Rule } = require('@core/lin-validator-v2.js')
const { ParameterException } = require('@core/http-exception.js')
const { User } = require('@model/user.js')
const { LoginType, ClassicType } = require('@lib/enum.js')

class RegisterValidator extends LinValidator {
  constructor () {
    super ()
    this.email = [
      new Rule('isEmail', '邮箱不符合规范')
    ]
    this.password1 = [
      new Rule('isLength', '最少6个字符，最长32个字符', {
        min: 6,
        max: 32
      })
    ]
    this.password2 = this.password1
  }
  validatePassword (ctx) {
    const password1 = ctx.body.password1
    const password2 = ctx.body.password2
    if (password1 !== password2) {
      throw new ParameterException('两次输入的密码不一样')
    }
  }
  async validateEmail (ctx) {
    const email = ctx.body.email
    const user = await User.findOne({
      where: {
        email
      }
    })
    if (user) {
      throw new ParameterException('邮箱已存在')
    }
  }
}

class TokenValidator extends LinValidator {
  constructor() {
    super()
    this.email = [
      new Rule('isEmail', '邮箱不符合规范'),
      new Rule('isOptional')
    ]
    this.password = [
      new Rule('isLength', '密码最少6个字符，最长32个字符', {
        min: 6,
        max: 32
      }),
      new Rule('isOptional')
    ]
  }
  validateType (ctx) {
    const type = ctx.body.type
    if (!type) {
      throw new ParameterException('type为必传参数')
    }
    if (!LoginType.isThisType(type)) {
      throw new ParameterException('type不符合规范')
    }
  }
}

class CannotEmptyValidator extends LinValidator {
  constructor () {
    super()
    this.token = [
      new Rule('isLength', '不能为空', {
        min: 1
      })
    ]
  }
}

class ClassicValidator extends LinValidator {
  constructor () {
    super()
    this.artId = [
      new Rule('isLength', 'artId是必传字段', {
        min: 1
      })
    ]
  }
  validateType (ctx) {
    const type = parseInt(ctx.path.type)
    if (!type) {
      throw new ParameterException('type是必传字段')
    }
    if (!ClassicType.isThisType(type)) {
      throw new ParameterException('type不符合规范')
    }
  }
}

class FavorValidator extends LinValidator {
  constructor () {
    super()
    this.artId = [
      new Rule('isLength', 'artId不能为空', {
        min: 1
      })
    ]
  }
  validateType (ctx) {
    const type = ctx.body.type
    if (!type) {
      throw new ParameterException('type为必传参数')
    }
    if (!ClassicType.isThisType(type)) {
      throw new ParameterException('type不符合规范')
    }
  }
}

class ArtIndexValidator extends LinValidator {
  constructor () {
    super()
    this.index = [
      new Rule('isInt', '不符合规范', {
        min: 1,
        max: 10
      })
    ]
  }
}

class BookDateValidator extends LinValidator {
  constructor () {
    super()
    this.date = [
      new Rule('isDate', '请输入这种类型的日期：2021-08-18')
    ]
  }
  validateDate (ctx) {
    const date = ctx.path.date
    if (!/-/.test(date)) {
      throw new ParameterException('请输入这种类型的日期：2021-08-18')
    }
  }
}

module.exports = {
  RegisterValidator,
  TokenValidator,
  CannotEmptyValidator,
  ClassicValidator,
  FavorValidator,
  ArtIndexValidator,
  BookDateValidator
}